<?php

namespace app\modules\web\controllers;

use yii\web\Controller;
use \app\modules\web\controllers\common\BaseController;
use \app\models\member\Member;
use \app\models\member\MemberComments;
use \app\common\services\ConstantMapService;
class MemberController extends BaseController
{
  
    public function actionIndex()
    {
    	
        $query=Member::find();
        $status=trim($this->get("status","1"));
        $mix_kw=trim($this->get("mix_kw",""));
        if($status > ConstantMapService::$status_default){
            
            $query->andWhere(["status"=>$status]);
        }
        if($mix_kw){
            $name=["LIKE","nickname","%".$mix_kw."%",false];
            $mobile=["LIKE","mobile","%".$mix_kw."%",false];
            $query->andWhere(["OR",$name,$mobile]);
        }
        //分页
        $p=($this->get("p",1));
        $total_all=$query->count();
        $page_size=20;
        $total_page=ceil($total_all/$page_size);
        $list=$query->orderBy(['id'=>SORT_DESC])
                ->offset( ($p-1)*$page_size )
                ->limit($page_size)
                ->all();
        $status_mapping=ConstantMapService::$status_mapping;
        return $this->render('index',["info"=>$list,'status_mapping'=>$status_mapping,'page'=>[
                'total_all'=>$total_all,
                'total_page'=>$total_page,
                'page_size'=>$page_size,
                'p'=>$p
            ]]);
    }
    public function actionSet()
    {
    	$this->layout="main";
        if(\Yii::$app->request->isGet){
            $id=$this->get("id",0);
            $info=Member::findOne($id);
            return $this->render('set',['info'=>$info]);
        }
        $nickname=trim($this->post("nickname",""));
        $mobile=trim($this->post("mobile",""));
        $id=$this->post("id",0);
        $info=Member::find();
        $res=$info->where(['id'=>$id])->one();
        //如果存在则更新,不存在则添加数据
        if($res){
            $info=$res;
        }else{
            $info= new Member();
            $info->created_time=time();
        }
        $info->nickname=$nickname;
        $info->mobile=$mobile;
        $info->updated_time=time();
        $info->save(0);
        return $this->renderJson([],"操作成功");
    }
    public function actionInfo()
    {
        $this->layout="main";
        return $this->render('info');
    }
    public function actionComment()
    {
        $id=$this->get('id','');
        if(!$id){
           $list=MemberComments::find()->join('inner join','book','book.id=member_comments.book_id')->join('inner join','member','member_comments.member_id=member.id')->select('*')->asArray()->all();
        }else{
            $list=MemberComments::find()->join('inner join','book','book.id=member_comments.book_id')->join('inner join','member','member_comments.member_id=member.id')->select('*')->where(['member_id'=>$id])->asArray()->all();
        }
        // var_dump($list);
        // die;
        return $this->render('comment',['list'=>$list]);
    }
    public function actionOps(){
        $id=$this->post('id','');
        if(!\Yii::$app->request->isPost){
            return $this->renderJson([],"系统繁忙~请稍后再试~",-1);
        }
        $res=Member::findOne($id);
        $status=$res['status'];
        $status==1?$status=0:$status=1;
        $res->status=$status;
        $res->save(0);
        return $this->renderJson([],"操作成功");
    }
}
